home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 402 < prev    next >
Internet Message Format  |  1996-08-06  |  3KB

  1. Path: solon.com!not-for-mail
  2. From: actuary@nando.net (Bill McCarthy)
  3. Newsgroups: comp.std.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion.
  5. Date: 15 Feb 1996 09:34:19 -0600
  6. Organization: News & Observer Public Access
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4fvjpr$enh@solutions.solon.com>
  10. References: <4fstj7$2l6@solutions.solon.com>
  11. Reply-To: actuary@nando.net (Bill McCarthy)
  12. NNTP-Posting-Host: solutions.solon.com
  13. X-Newsreader: IBM NewsReader/2 v1.2
  14.  
  15. In <4fstj7$2l6@solutions.solon.com>,
  16. Rune Huseby <rune.huseby@gpi.telemax.no> writes:
  17.  
  18. :I got a problem understanding the rules on integral promotion in 
  19. :the C language:
  20. :
  21. :Consider the following function:
  22. :
  23. :short test(short x1, short x2);
  24. :
  25. :int main(void)
  26. :{
  27. :  short result;
  28. :  result = test(1, 2);
  29. :  return 0;
  30. :}
  31. :
  32. :short test(short x1, short x2)
  33. :{
  34. :  short result;
  35. :  result = x1 + x2;  /* Warning:  '=' : conversion from 'int '                                 
  36. :                         to 'short ', possible loss of data */
  37. :  return result;
  38. :}
  39. :
  40. :My compiler (Microsoft Visual C++ 4.0), automagically converts 
  41. :my short-parameters to int's, even though all variables involved 
  42. :are short. I know that the standard says that all arguments can 
  43. :be converted to the biggest 'type' of all the arguments, but is 
  44. :it correct that char's and short's always are promoted to int's, 
  45. :without regard to the other arguments in the expression?
  46.  
  47. No.  Char's and int's will only be promoted to int's if an int can
  48. represent all values of the original type.  Otherwise they will
  49. be promoted to unsigned int's.  This process is called Integral
  50. Promotion and is described in A6.1 of K&R II.  You should read
  51. the entire section A.6 Conversions (less than three pages) and
  52. pay special attention to A6.5 Arithmetic Conversions, particu-
  53. larly if you are switching between difference environments.
  54.  
  55. The rules of dealing with one operand a long and the other an
  56. unsigned, differ depending on whether all values of an unsigned
  57. call be represented by a long.  They can be on most 16 bit
  58. implementations (short=int=16-bit, long=32-bit), but cannot on
  59. most 32 bit (short=16, int=long=32-bit).
  60.  
  61. :The reason I ask is that I am writing code that should be 
  62. :portable from a 16-bits environment (where short and int are the 
  63. :same size) to a 32-bits environment. (My 16-bits compiler does 
  64. :not complain about the code).
  65.  
  66. Since, as you state, short and int are the same size on your 16 bit
  67. compiler, you can assign from signed int to signed short without
  68. "possible loss of data."
  69.  
  70. Bill McCarthy
  71. actuary@nando.net
  72. Wendell, NC  USA
  73.